# Demonstrates the use of Progress Bar (Changing A.Hadi)
# Also shows how the Timer class is used in PySide

import sys
from PySide import QtGui, QtCore


class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()

        self.setGeometry(200, 200, 300, 200)
        self.setWindowTitle("Progress Bars")

        self.pbar = QtGui.QProgressBar(self)
        self.pbar.setGeometry(30, 40, 200, 25)

        self.btn = QtGui.QPushButton("Start", self)
        self.btn.move(40, 80)
        self.btn.clicked.connect(self.doAction)

        # To activate the progress bar, we use the timer object.
        self.tmr = QtCore.QBasicTimer()

        # Make a variable named 'step' and initialize it to 0
        self.step = 0

    # This method will execute in each timer event.
    # On each timerEvent e, increase the step by one. If the number of steps equal 100, then stop the timer and set
    # the text of the push button to "Finished"
    # Each QtCore.QObject and its descendants have a timerEvent() event handler. In order to react to timer events, we
    # re-implement the event handler. We update the self.step variable and set a new value for the progress bar widget.
    def timerEvent(self, e):
        if self.step >= 100:
            self.tmr.stop()
            self.btn.setText("Finished")
            return
        self.step = self.step + 1
        self.pbar.setValue(self.step)

    # The doAction() method will execute when the Start/Stop push button is pressed
    # Inside the doAction() method we start and stop the timer
    def doAction(self):
        if self.tmr.isActive():
            self.tmr.stop()
            self.btn.setText("Start")
        else:
            # start(msec, obj) - Generate a timer event every 100 msec
            self.tmr.start(100, self)
            self.btn.setText("Stop")

if __name__ == "__main__":
    appObject = QtGui.QApplication(sys.argv)
    ex = Window()
    ex.show()
    sys.exit(appObject.exec_())
